package manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manage.dao.AttendanceMapper;
import manage.dao.UserMapper;
import manage.model.Attendance;
import manage.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private UserMapper userMapper;

    public PageInfo<Attendance> selectAll(String page,String limit) {
        int li = Integer.parseInt(limit);
        int pu = Integer.parseInt(page);
        PageHelper.startPage(pu,li);
        List<Attendance> list = attendanceMapper.selectAll();
        PageInfo<Attendance> pageInfo = new PageInfo<Attendance>(list);
        pageInfo.setPageNum(pageInfo.getPageNum());
        return pageInfo;
    }
    public PageInfo<Attendance> selectAll(String page,String limit,String account,String date) {
        int li = Integer.parseInt(limit);
        int pu = Integer.parseInt(page);
        PageHelper.startPage(pu,li);
        List<Attendance> list = attendanceMapper.selectAny(account,date);
        PageInfo<Attendance> pageInfo = new PageInfo<Attendance>(list);
        pageInfo.setPageNum(pageInfo.getPageNum());
        return pageInfo;
    }

    public PageInfo<Attendance> selectAny(String page,String limit,String value) {
        int li = Integer.parseInt(limit);
        int pu = Integer.parseInt(page);
        PageHelper.startPage(pu,li);
        List<Attendance> list = attendanceMapper.selectAny1(value);
        PageInfo<Attendance> pageInfo = new PageInfo<Attendance>(list);
        pageInfo.setPageNum(pageInfo.getPageNum());
        return pageInfo;
    }

    public String insert(Attendance attendance) {
        if (userMapper.selectByPrimaryKey(attendance.getUserId())==null){

            return "员工不存在";
        }
        if(attendanceMapper.selectByDateRecord(attendance.getDateRecord(),attendance.getUserId())!=null){

            return "本日记录已存在";
        }
        int i = attendanceMapper.insert(attendance);
        if (i>0){
            return "success";
        }
        return "false";
    }
}
